package ar.uade.fain.ai.tpo.view.seccion;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import ar.uade.fain.ai.tpo.Sistema;
import ar.uade.fain.ai.tpo.dto.ProductoDTO;
import ar.uade.fain.ai.tpo.dto.SeccionDTO;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificarSeccion extends javax.swing.JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private JButton modificarSeccionButton;
	private JButton addStockButton;
	private JTable stock;
	private JScrollPane scroll;
	private JButton findButton;
	private JTextField nombreText;
	private JLabel nombreLabel;
	private JTextField idText;
	private JLabel idLabel;
	private DefaultTableModel stockModel;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ModificarSeccion inst = new ModificarSeccion();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ModificarSeccion() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				modificarSeccionButton = new JButton();
				getContentPane().add(modificarSeccionButton);
				modificarSeccionButton.setText("Modificar");
				modificarSeccionButton.setBounds(240, 267, 137, 23);
				modificarSeccionButton.addActionListener(new ActionListener() {
					@SuppressWarnings("unchecked")
					@Override
					public void actionPerformed(ActionEvent e) {
						final SeccionDTO seccionDTO = new SeccionDTO();
						seccionDTO.setId(Long.valueOf(idText.getText()));
						seccionDTO.setNombre(nombreText.getText());
						final HashMap<ProductoDTO, Integer> stockMap =
								new HashMap<ProductoDTO, Integer>();
						for (Vector<String> row : 
							((Vector<Vector<String>>)stockModel.getDataVector())) {
							ProductoDTO productoDTO = new ProductoDTO();
							productoDTO.setId(Long.valueOf(row.elementAt(0)));
							stockMap.put(productoDTO, Integer.valueOf(row.elementAt(1)));
						}
						seccionDTO.setStock(stockMap);
						//System.out.println(seccionDTO);
						Sistema.get().getSeccionControlador().modificarSeccion(seccionDTO);
						ModificarSeccion.this.dispose();
					}
				});
			}
			{
				idLabel = new JLabel();
				getContentPane().add(idLabel);
				idLabel.setText("Id de la seccion:");
				idLabel.setBounds(24, 30, 163, 16);
			}
			{
				idText = new JTextField();
				getContentPane().add(idText);
				idText.setBounds(159, 27, 107, 23);
			}
			{
				nombreLabel = new JLabel();
				getContentPane().add(nombreLabel);
				nombreLabel.setText("Nombre:");
				nombreLabel.setBounds(24, 58, 163, 16);
			}
			{
				nombreText = new JTextField();
				getContentPane().add(nombreText);
				nombreText.setBounds(159, 56, 219, 23);
				nombreText.setEditable(false);
			}
			{
				findButton = new JButton();
				getContentPane().add(findButton);
				findButton.setText("Buscar");
				findButton.setBounds(272, 27, 106, 24);
				findButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						String id = idText.getText();
						if (id != null && !id.isEmpty()) {
							SeccionDTO seccionDTO = Sistema.get().getSeccionControlador()
									.buscarSeccion(Long.valueOf(id));
							nombreText.setText(seccionDTO.getNombre());
							nombreText.setEditable(true);	
							for (int i = 0; i < stockModel.getRowCount(); i++) {
								stockModel.removeRow(i);
							}
							for (ProductoDTO dto : seccionDTO.getStock().keySet()) {
								stockModel.addRow(new String[] { 
										dto.getId().toString(), 
										seccionDTO.getStock().get(dto).toString()});	
							}
						}
					}
				});
			}
			{
				scroll = new JScrollPane();
				getContentPane().add(scroll);
				scroll.setBounds(23, 101, 354, 141);
				{
					this.stockModel = new DefaultTableModel();
					stockModel.addColumn("ID Producto");
					stockModel.addColumn("Cantidad");
					stock = new JTable();
					scroll.setViewportView(stock);
					stock.setModel(stockModel);
				}
			}
			{
				addStockButton = new JButton();
				getContentPane().add(addStockButton);
				addStockButton.setText("Agregar");
				addStockButton.setBounds(23, 268, 136, 22);
				addStockButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						stockModel.addRow(new String[]{"",""});
					}
				});
			}
			pack();
			this.setSize(407, 338);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
